package files;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;

public class ImageUtils {
    //Note that JPEG is not a good format for storing images with alpha channel. While it is possible,
    // most software will not display it properly. So I suggest using PNG instead.
    //Alternatively, you could remove the alpha channel, and use JPEG.
    public static void main(String[] args) throws IOException {
        // You need to know width/height of the image
        int width = 1024;
        int height = 1024;

        int samplesPerPixel = 4;
        int[] bandOffsets = {2, 1, 0, 3}; // BGRA order

        byte[] bgraPixelData = new byte[width * height * samplesPerPixel];

        DataBuffer buffer = new DataBufferByte(bgraPixelData, bgraPixelData.length);
        System.out.println("asdfasdflkkjj12121212");
        WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);

        ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
        BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
        System.out.println("image: " + image); // Should print: image: BufferedImage@<hash>: type = 0 ...
        String path = "d:/aaa.png";
        System.out.println("121212");
        System.out.println("121212");
        System.out.println("00000000");

        ImageIO.write(image, "PNG", new File(path));
    }
}
